package com.wenx.schedule;

import java.util.Map;
import java.util.concurrent.*;

/**
 * Created by wenx on 2019-08-14 09:33
 */
public class Demo2 {

    private static Map<String, ScheduledFuture> futureMap = new ConcurrentHashMap<>();
    private static ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1);

    public static void main(String[] args) {


        Beeper b = new Demo2().new Beeper();
        ScheduledFuture<?> scheduledFuture = schedule.scheduleAtFixedRate(b, 0, 5, TimeUnit.SECONDS);

        futureMap.put("11",scheduledFuture);

    }


    class Beeper implements Runnable {

        private int count = 1;

        @Override
        public void run() {
            if(count > 5) {
                System.out.println("关闭");
                ScheduledFuture scheduledFuture = futureMap.get("11");
                scheduledFuture.cancel(false);
                ScheduledFuture<?> scheduledFuture1 = schedule.scheduleAtFixedRate(new Beeper(), 2, 2, TimeUnit.SECONDS);
                futureMap.put("11",scheduledFuture1);
            }
            System.out.println(" count = " + count);
            count++;
        }
    }
}